using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace eTMS.Controls
{
    public class TextBox : System.Windows.Forms.TextBox
    {
        private bool _IsEnterSelectNext = true;
        public event EventHandler OnPressEnter;
        public event EventHandler OnPressLeft;
        public event EventHandler OnPressRight;
        public event EventHandler OnPressUp;
        public event EventHandler OnPressDown;
        public event EventHandler OnPressPageDown;
        public event EventHandler OnPressPageUp;
        public bool IsEnterSelectNext
        {
            get
            {
                return _IsEnterSelectNext;
            }
            set
            {
                _IsEnterSelectNext = value;
            }
        }
        private Control _PrevText;
        private Control _NextText;
        public Control PrevText
        {
            set
            {
                _PrevText = value;
            }
            get
            {
                return _PrevText;

            }
        }
        public Control NextText
        {
            set
            {
                _NextText = value;
            }
            get
            {
                return _NextText;

            }
        }
        private void SendTab()
        {
            SendKeys.Send("{TAB}");
        }
        private void SendShiftTab()
        {
            SendKeys.Send("{SHIFT}{TAB}");
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (OnPressEnter != null) OnPressEnter(this, e);
                    if (_IsEnterSelectNext)
                    {
                        if (_NextText != null)
                        {
                            if (_NextText is Button)
                            {
                                ((Button)_NextText).PerformClick();
                            }
                            else
                            {
                                _NextText.Focus();
                            }
                        }
                        else
                        {

                            SendTab();
                        }
                        e.Handled = true;
                    }
                    break;
                case Keys.Left:
                    if (OnPressLeft != null) OnPressLeft(this, e);
                    if ((_PrevText != null) && (this.SelectionStart == 0))
                    {
                        if (_PrevText != null)
                        {
                            _PrevText.Focus();
                        }
                        else
                        {
                            SendShiftTab();
                        }
                    }
                    break;
                case Keys.Right:
                    if (OnPressRight != null) OnPressRight(this, e);
                    if ((_NextText != null) && (this.SelectionStart == this.Text.Length))
                    {
                        if (_NextText != null)
                        {
                            _NextText.Focus();
                        }
                        else
                        {
                            SendTab();
                        }
                    }
                    break;
                case Keys.Up:
                    if (OnPressUp != null) OnPressUp(this, e);
                    break;
                case Keys.Down:
                    if (OnPressDown != null) OnPressDown(this, e);
                    break;
                case Keys.PageDown:
                    if (OnPressPageDown != null) OnPressPageDown(this, e);
                    break;
                case Keys.PageUp:
                    if (OnPressPageUp != null) OnPressPageUp(this, e);
                    break;
            }
        }
        public TextBox()
        {
            mclrBorder = Color.FromArgb(140, 180, 205);
            mclrHighlight = Color.FromArgb(255, 205, 120);
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        }

        private Color mclrBorder;
        private Color mclrHighlight;
        public Color BorderColor
        {
            get
            {
                return mclrBorder;
            }
            set
            {
                mclrBorder = value;
            }
        }
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.Invalidate();
        }
        protected override void OnValidated(EventArgs e)
        {
            base.OnValidated(e);
            this.Invalidate();
        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            this.Invalidate();
        }

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            base.WndProc(ref m);
            if (m.Msg != eTMS.Commons.Win32.WM_PAINT)
            {
                return;
            }
            Rectangle rec = this.ClientRectangle;
            Pen pen = null;
            if (!DesignMode && (!ReadOnly) && Enabled && (Focused))
                pen = new Pen(mclrHighlight);
            else
                pen = new Pen(mclrBorder);
            IntPtr hDc = eTMS.Commons.Win32.GetWindowDC(Handle);
            Graphics g = Graphics.FromHdc(hDc);
            g.DrawRectangle(pen, 0, 0, rec.Width - 1, rec.Height - 1);
            g.Dispose();
            pen.Dispose();
            eTMS.Commons.Win32.ReleaseDC(Handle, hDc);
        }
    }
}
